const paths = require('./paths');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [path.resolve(__dirname, '../src')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: false,
  }
});

module.exports = function(options) {
  return {
    mode: options.mode,
    entry: paths.appSrc,
    output: {
      path: paths.appBuild,
      publicPath: '',
    },
    cache: {
      // 使用持久化缓存
      type: 'filesystem', //memory:使用内容缓存 filesystem：使用文件缓存
    },
    devtool: false,
    module: {
      rules: [
        ...[createLintingRule()],
        {
          test: /\.(sa|sc|le|c)ss$/,
          use: ['style-loader', 'css-loader', 'less-loader'],
        },
        {
          test: /\.png|\.jpg$/,
          use: {
            //将图片转为base64内容，适用于较小图片资源
            loader: 'url-loader',
            options: {
              limit: 10 * 1024, // 10kb
              esModule: false, // src="[object Module]"
            },
          },
        },
        {
          test: /\.vue$/,
          loader: 'vue-loader',
        },
        {
          test: /\.js|(\.vue)$/,
          loader: 'eslint-loader',
          enforce: 'pre',
        },
      ],
    },
    devServer: options.devServer || {},
    plugins: [
      new VueLoaderPlugin(),
      new HtmlWebpackPlugin({
        filename: 'index.html',
        title: 'vue-cli',
        url: './',
        template: './public/index.html',
      }),
      ...options.plugins,
    ],
    stats: options.stats, // 打包日志发生错误和新的编译时输出
  };
};


function resolve (dir) {
  return path.join(__dirname, '..', dir)
}